<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { contentApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    screenPages:[] as Array<any>,
  })
  interface formQuert {
    id:number
    name:string
    show_pages:string
    bg_img_url:string
    action_img_url:string
    action:string
    text:string
    room_ids:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    name: [{ required: true, message: "请输入名称", trigger: "blur" }],
    text: [{ required: true, message: "请输入飘屏文案", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id } = form
      let newForm = {...form}
      newForm.show_pages = newForm.show_pages.toString()
      if(id) {
        await contentApi.screenListEdit(newForm).then(()=>{
          ToastSuccess('编辑成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }else {
        await contentApi.screenListAdd(newForm).then(()=>{
          ToastSuccess('新增成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = async(query:any = {})=> {
    let {
      id=0, name, show_pages, screenPages=[],
      action_img, action:action_url, bg_img, text, room_ids=''
    } = query
    let arr = []
    if(show_pages) {
      arr = show_pages.split(',').map((res:string)=>{
        return Number(res)
      })
    }
    Object.assign(form,{
      id,
      name,
      show_pages: arr || [],
      bg_img_url:bg_img,
      action_img_url:action_img,
      action:action_url,
      text,
      room_ids
    })
    action.title = id?'编辑飘萍':'新增飘萍'
    action.screenPages = screenPages
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width xm-dialog-cont-y"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="52%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="展示位置：">
          <el-checkbox-group v-model="form.show_pages">
            <el-checkbox
              name="type"
              v-for="(key,val) of action.screenPages"
              :label="val">{{key}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="背景图片">
          <xmUpload
            listType="picture-card"
            :data="{type:21}"
            v-model:file-url="form.bg_img_url"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="跳转图片">
          <xmUpload
            listType="picture-card"
            :data="{type:21}"
            v-model:file-url="form.action_img_url"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="跳转地址">
          <el-input
            v-model="form.action"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入跳转地址"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="飘屏文案" prop="text">
          <el-input
            v-model="form.text"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入飘屏文案"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="房间ID">
          <el-input
            v-model="form.room_ids"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入房间ID，如果有多个，用英文逗号分隔"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>